/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Whitemice.ZOGI.GtkFolios;

namespace Whitemice.Consonance
{
  /// <summary>
  /// executant search dialog
  /// </summary>
  public partial class ExecutantSearchDialog : Gtk.Dialog, IZOGIWidget
  {
    public delegate void SetExecutantIdHandler(int executantId, string description);
    
    public    SetExecutantIdHandler              SetExecutantId;
    protected MultiResultCallback                callback;
    
    public ExecutantSearchDialog() : this(null, 0, string.Empty)
    {
    }
    
    public ExecutantSearchDialog(Window parent, int taskId, string title)
      : base(title, parent, Gtk.DialogFlags.DestroyWithParent)
    {
      this.Build();
      callback = new MultiResultCallback(this.ReceiveDataCallback);
      SearchTeams();
    }

    protected virtual void OnEntityChanged (object sender, System.EventArgs e)
    {
      list.Clear();
      if (entity.Active == 0)
      {
      } else
        {
          // account mode
        }
    } // end OnEntityChanged
    
    protected virtual void SearchTeams()
    {
      TeamSearchCommand  command;

      command = new TeamSearchCommand(65535, "ExecutantSearch:Team");
      command.Callback = callback;
      Server.Connection.RunCommand(command);
    } // end SearchTeams

    protected virtual void OnFindClicked (object sender, System.EventArgs e)
    {
      Console.WriteLine("ExecutantSearchDialog.OnFindClicked");
      this.Modal = true;
      entity.Sensitive = false;
      find.Sensitive = false;
      entry.Sensitive = false;
      if(entity.Active == 0)
      {
        SearchTeams();
      } else
        {
          ContactSearchCommand  command;

          command = new ContactSearchCommand("ExecutantSearch:Login");
          command.Callback = callback;
          command.AddANDCriteria("isAccount", 1, false, false);
          command.AddANDCriteria("lastname",
                                 string.Format("*{0}*", entry.Text.Trim()),
                                 true,
                                 true);
          Server.Connection.RunCommand(command);
        }
    } // end OnFindClicked
        
    public void ReceiveRPCCall(RPCEventArgs args)
    {
      list.Clear();
      if (args.HasResults)
      {
        foreach(Entity t in args.Payload)
          list.AppendEntity(t);
      } else if (args.IsError)
        {
          //todo: handle error
        } else
          {
            //todo: handle empty results
          }
      this.Modal = false;
      list.Sensitive = true;
      entry.Sensitive = true;
      find.Sensitive = true;
      entity.Sensitive = true;
    } // end SearchResultCallback

    protected virtual void OnOkClicked (object sender, System.EventArgs e)
    {
      Entity selectedEntity;
      
      selectedEntity = list.SelectedEntity;
      if (selectedEntity != null)
      {
        if (selectedEntity.EntityName == "Team")
        {
          // send team as executant
          SetExecutantId(
            selectedEntity.ObjectId, 
            (selectedEntity as Team).Name);
        } else
          {
            // send account as executant
            SetExecutantId(
              selectedEntity.ObjectId,
              string.Format("{0}, {1} ({2})",
                (selectedEntity as Contact).LastName,
                (selectedEntity as Contact).FirstName,
                (selectedEntity as Contact).Login));
          }
        this.Destroy();
      } else new MessageBox("An executant must be selected.", MessageType.Info);
    } // end OnOkClicked

    protected virtual void OnCancelClicked (object sender, System.EventArgs e)
    {
      this.Destroy();
    }
  } // end ExecutantSearchDialog
} // end namespace
